﻿using BCCommon;
using BCData.Sim.Admin.Admin;
using BCData.Sim.Admin.AdminPermissionRecord;
using BCData.Sim.Admin.StaffOrganizationMappingRecord;
using BCData.Sim.Company.Department;
using BCDto.Sim.Admin.Admin;
using BCDto.Sim.Admin.AdminPermissionRecord;
using BCEntity.Common.EntityNameConst;
using BCEntity.Sim.Admin.AdminPermissionRecord;
using BCService.Utils;
using System;
using System.Collections.Generic;

namespace BCService.Sim.Admin.AdminPermissionRecord
{
    public class AdminPermissionRecordService : IAdminPermissionRecordService
    {
        private readonly IAdminPermissionRecordData adminPermissionRecordData;
        private readonly IAdminData adminData;
        private readonly IStaffOrganizationMappingRecordData staffOrganizationMappingRecordData;
        private readonly IRedisService redisService;

        public AdminPermissionRecordService(IAdminPermissionRecordData adminPermissionRecordData,
            IAdminData adminData,
            IStaffOrganizationMappingRecordData staffOrganizationMappingRecordData,
            IRedisService redisService)
        {
            this.adminPermissionRecordData = adminPermissionRecordData;
            this.adminData = adminData;
            this.staffOrganizationMappingRecordData = staffOrganizationMappingRecordData;
            this.redisService = redisService;
        }

        public AdminPermissionRecordDto Setting(AdminPermissionRecordRequestDto requestDto)
        {
            string key = string.Format("{0}:AdminId={1}", EntityNameConst.AdminPermissionRecordListEntity, requestDto.AdminId);
            if (requestDto.AdminId == 0 || adminData.GetAdmin(requestDto.AdminId) == null)
            {
                throw new ArgumentException("员工信息不存在");
            }
            if (string.IsNullOrWhiteSpace(requestDto.OperationPermission.Trim()))
            {
                throw new ArgumentException("操作权限不能为空");
            }
            if (!requestDto.OperationPermission.Contains(".", StringComparison.CurrentCulture))
            {
                throw new ArgumentException("操作权限格式错误");
            }
            string[] operationPermission = requestDto.OperationPermission.Split(".");
            string permissionCode = operationPermission[0];
            string operstion = operationPermission[1];
            AdminPermissionRecordEntity entity = new AdminPermissionRecordEntity
            {
                PermissionCode = permissionCode,
                AdminId = requestDto.AdminId,
                StaffOrganizationMappingRecordId = requestDto.StaffOrganizationMappingRecordId
            };
            var recordEntity = this.adminPermissionRecordData.GetEntity(requestDto.AdminId, permissionCode).GetAwaiter().GetResult();
            if (recordEntity != null)
            {
                entity.AdminPermissionRecordId = recordEntity.AdminPermissionRecordId;
                entity.EnabledInsert = recordEntity.EnabledInsert;
                entity.EnabledUpdate = recordEntity.EnabledUpdate;
                entity.EnabledDelete = recordEntity.EnabledDelete;
                entity.EnabledSelect = recordEntity.EnabledSelect;
                switch (operstion)
                {
                    case "Insert":
                        entity.EnabledInsert = !recordEntity.EnabledInsert;
                        break;
                    case "Update":
                        entity.EnabledUpdate = !recordEntity.EnabledUpdate;
                        break;
                    case "Delete":
                        entity.EnabledDelete = !recordEntity.EnabledDelete;
                        break;
                    default:
                        entity.EnabledSelect = !recordEntity.EnabledSelect;
                        break;
                }
                var result = this.adminPermissionRecordData.Update(entity).GetAwaiter().GetResult().As<AdminPermissionRecordDto>();
                if (result != null)
                {
                    this.redisService.DeleteAsync(key);
                    string mappingKey = string.Format("{0}", EntityNameConst.AdminPermissionMappingPageViewDto);
                    string departmentKey = string.Format("{0}", EntityNameConst.DepartmentPermissionListEntity);
                    string adminRecordKey = string.Format("{0}", EntityNameConst.AdminPermissionRecordListEntity);
                    this.redisService.DeleteMultipleKey(mappingKey, false);
                    this.redisService.DeleteMultipleKey(departmentKey, false);
                    this.redisService.DeleteMultipleKey(adminRecordKey, false);
                }
                return result;
            }
            else
            {
                entity.EnabledInsert = false;
                entity.EnabledUpdate = false;
                entity.EnabledDelete = false;
                entity.EnabledSelect = false;
                switch (operstion)
                {
                    case "Insert":
                        entity.EnabledInsert = true;
                        break;
                    case "Update":
                        entity.EnabledUpdate = true;
                        break;
                    case "Delete":
                        entity.EnabledDelete = true;
                        break;
                    default:
                        entity.EnabledSelect = true;
                        break;
                }
                var result = this.adminPermissionRecordData.Add(entity).GetAwaiter().GetResult().As<AdminPermissionRecordDto>();
                if (result != null)
                {
                    this.redisService.DeleteAsync(key);
                    string mappingKey = string.Format("{0}", EntityNameConst.AdminPermissionMappingPageViewDto);
                    string departmentKey = string.Format("{0}", EntityNameConst.DepartmentPermissionListEntity);
                    string adminRecordKey = string.Format("{0}", EntityNameConst.AdminPermissionRecordListEntity);
                    this.redisService.DeleteMultipleKey(mappingKey, false);
                    this.redisService.DeleteMultipleKey(departmentKey, false);
                    this.redisService.DeleteMultipleKey(adminRecordKey, false);
                }
                return result;
            }
        }

        public IEnumerable<AdminPermissionRecordDto> GetList(long staffOrganizationMappingRecordId)
        {
            return this.adminPermissionRecordData.GetList(staffOrganizationMappingRecordId).GetAwaiter().GetResult().As<IEnumerable<AdminPermissionRecordDto>>();
        }

        public AdminPermissionRecordDto Delete(long staffOrganizationMappingRecordId, string operationPermissionRequest)
        {
            var staffEntity = this.staffOrganizationMappingRecordData.GetEntity(staffOrganizationMappingRecordId).GetAwaiter().GetResult();
            if (staffEntity == null)
            {
                throw new ArgumentException("员工角色信息不存在");
            }
            string key = string.Format("{0}:AdminId={1}", EntityNameConst.AdminPermissionRecordListEntity, staffEntity.AdminId);
            if (staffEntity.AdminId == 0 || this.adminData.GetAdmin(staffEntity.AdminId) == null)
            {
                throw new ArgumentException("员工信息不存在");
            }

            if (string.IsNullOrWhiteSpace(operationPermissionRequest.Trim()))
            {
                throw new ArgumentException("操作权限不能为空");
            }
            if (!operationPermissionRequest.Contains(".", StringComparison.CurrentCulture))
            {
                throw new ArgumentException("操作权限格式错误");
            }
            string[] operationPermission = operationPermissionRequest.Split(".");
            string permissionCode = operationPermission[0];
            string operstion = operationPermission[1];
            var recordEntity = this.adminPermissionRecordData.GetEntity(staffEntity.AdminId, permissionCode).GetAwaiter().GetResult();
            if (recordEntity == null)
            {
                throw new ArgumentException("员工权限记录信息不存在");
            }
            AdminPermissionRecordEntity entity = new AdminPermissionRecordEntity
            {
                PermissionCode = permissionCode,
                AdminId = staffEntity.AdminId,
                AdminPermissionRecordId = recordEntity.AdminPermissionRecordId,
                StaffOrganizationMappingRecordId = staffOrganizationMappingRecordId
            };
            switch (operstion)
            {
                case "Insert":
                    entity.EnabledInsert = false;
                    break;
                case "Update":
                    entity.EnabledUpdate = false;
                    break;
                case "Delete":
                    entity.EnabledDelete = false;
                    break;
                default:
                    entity.EnabledSelect = false;
                    break;
            }
            var result = this.adminPermissionRecordData.Update(entity).GetAwaiter().GetResult().As<AdminPermissionRecordDto>();
            this.redisService.DeleteAsync(key);
            string mappingKey = string.Format("{0}", EntityNameConst.AdminPermissionMappingPageViewDto);
            string departmentKey = string.Format("{0}", EntityNameConst.DepartmentPermissionListEntity);
            string adminRecordKey = string.Format("{0}", EntityNameConst.AdminPermissionRecordListEntity);
            this.redisService.DeleteMultipleKey(mappingKey, false);
            this.redisService.DeleteMultipleKey(departmentKey, false);
            this.redisService.DeleteMultipleKey(adminRecordKey, false);
            return result;
        }

    }
}
